package com.bg.print.config.securityconfig;

import com.alibaba.fastjson2.JSON;
import com.bg.print.utils.R;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

import java.io.IOException;

/**
 * @program: bg-print-java
 * @description: 会话失效
 * @author: liuxvru
 * @create: 2024-04-03 15:45
 **/

public class MySessionInformationExpiredStrategy implements SessionInformationExpiredStrategy {
    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {

        //转换成json字符串
        String json = JSON.toJSONString(R.error(401,"该账号已从其他设备登录"));

        HttpServletResponse response = event.getResponse();
        //返回响应
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(json);
    }
}